/*
 * @Author: your name
 * @Date: 2022-04-22 14:55:27
 * @LastEditTime: 2022-04-25 09:38:19
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \mallAdmin\src\app\main\used-store\used-marketing-manage\second-descent-details\second-descent-details.component.ts
 */
import { Component, OnInit, ViewChild } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { sencondDescentStatusArrs } from '../used-marketing-manage.pipe';
import { PermissionService } from '../../../../service/permission.service';
import { SecondDescentService } from '../services/second-descent.service';

@Component({
  selector: 'app-second-descent-details',
  templateUrl: './second-descent-details.component.html',
  styleUrls: ['./second-descent-details.component.scss']
})
export class SecondDescentDetailsComponent implements OnInit {
  @ViewChild('followUpRef') followUpRef: any;

  routeParams: any = {};
  detailDatas: any = {};
  recordStatusOptions: any = [...sencondDescentStatusArrs];

  useTableParams: any = {
    loading: false,
    status: '',
    tableData: [],
    page: 1,
    pageSize: 10,
    total: 0
  };

  discountTable: any = [];

  // 跟进 Modal
  initParams: any = {
    type: 1, // type  1、订单 2、售后 3、客户 4、出库 5、直降 6、退税 7、机器 8、商品
    modelId: 2, // 类型 1、旧的表示二手回收 2、表示二手商城 3、表示二手回收
    relationInformation: ''
  };

  constructor(
    private message: NzMessageService,
    private modal: NzModalService,
    private activatedRoute: ActivatedRoute,
    public permission: PermissionService,
    private services: SecondDescentService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.loadDetailData();
    this.loadRecordTable();
  }

  // 详情
  async loadDetailData() {
    let params = {
      id: this.routeParams.id,
      page: 1,
      pageSize: 1
    }
    const messId = this.message.loading('加载中..', { nzDuration: 0 }).messageId;
    const { code, data, message }: any = await this.services.getDescentDetail(params);
    this.message.remove(messId);

    if (code === 0) {
      const { promotionInfo } = data || {};
      this.detailDatas = promotionInfo || {};
      this.discountTable = promotionInfo.discountContent ? JSON.parse(promotionInfo.discountContent) : [];
    } else {
      this.message.error(message);
    }
  }

  // 使用记录 Table
  async loadRecordTable() {
    let params = {
      id: this.routeParams.id,
      status: this.useTableParams.status,
      page: this.useTableParams.page,
      pageSize: this.useTableParams.pageSize
    }

    this.useTableParams.loading = true;
    const { code, data, message }: any = await this.services.getDescentDetail(params);
    this.useTableParams.loading = false;

    if (code === 0) {
      const { records, total }: any = data?.list || {};
      this.useTableParams.tableData = records || [];
      this.useTableParams.total = total || 0;
    } else {
      this.message.error(message);
    }
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.useTableParams.page = index;
    this.loadRecordTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.useTableParams.page = 1;
    this.useTableParams.pageSize = sizeNum;
    this.loadRecordTable();
  }

  statusChange(val: any) {
    this.useTableParams.page = 1;
    this.loadRecordTable();
  }

  // 跟进
  showFollowModal(item: any) {
    this.initParams.relationInformation = item.orderNum;
    this.followUpRef.isModelVisible = true;
  }

  // 取消订单
  cannlOrder(item: any) {
    this.modal.confirm({
      nzTitle: '取消订单',
      nzContent: '<b style="color: red;">确认取消？该操作不可逆，请提前和用户沟通</b>',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        let messId = this.message.loading('订单取消中..', { nzDuration: 0 }).messageId;
        const res: any = await this.services.cancelOrderData({ orderId: item.orderId, status: 11 });
        this.message.remove(messId);

        if (res.code == 0) {
          this.message.success('取消成功');
          this.loadRecordTable();
        } else {
          this.message.error(res.message || '取消失败');
        }
      },
      nzCancelText: '取消'
    });
  }
}
